package net.fdukedom.epicurus.client.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

public class GridBagJPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Main container
	 */
	private Container container;

	/**
	 * Layout of main container
	 */
	private GridBagLayout layout;
	
	public GridBagJPanel(){
	}

	/**
	 * Initialization of layout.
	 * 
	 * @param columnWeights
	 *            weights of columns.
	 * @param columnWidths
	 *            widths of columns.
	 * @param rowWeights
	 *            weights of rows.
	 * @param rowHeights
	 *            heights of rows.
	 */
	protected void initLayout(final double[] columnWeights,
			final int[] columnWidths, final double[] rowWeights,
			final int[] rowHeights) {
		container = this;
		layout = new GridBagLayout();

		layout.columnWeights = columnWeights;
		layout.columnWidths = columnWidths;
		layout.rowWeights = rowWeights;
		layout.rowHeights = rowHeights;
		container.setLayout(layout);
	}

	/**
	 * Adds component to frame.
	 * 
	 * @param component
	 *            component to add
	 * @param row
	 *            row in grid
	 * @param column
	 *            column in grid
	 * @param width
	 *            width of component
	 * @param height
	 *            height of component
	 */
	public void addComponent(final Component component, final int row,
			final int column, final int width, final int height, final int anchor) {
		GridBagConstraints constrains = new GridBagConstraints();
		constrains.gridx = column;
		constrains.gridy = row;
		constrains.gridwidth = width;
		constrains.gridheight = height;
		constrains.anchor = anchor;
		constrains.fill = GridBagConstraints.HORIZONTAL;
		layout.setConstraints(component, constrains);
		container.add(component);
	}

}
